package vn.edu.uit.chemistry;

import vn.edu.uit.chemistry.utilities.TCPServerCommand;
import vn.edu.uit.chemistry.utilities.TCPServerReceiver;
import android.app.TabActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;

public class MainActivity extends TabActivity {

	TextView tvTitle;
	
	TabHost tabHost = null;
	Button btnBaiGiang;
	Button btnKiemTra;
	Button btnGiaiTri;
	Button btnThietLap;
	
	TCPServerReceiver serverTransfer;
	TCPServerCommand serverCommand;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		serverCommand = new TCPServerCommand(this);
		serverCommand.start();
		
		serverTransfer = new TCPServerReceiver(this);
		serverTransfer.start();
		
		tabHost = getTabHost();
		setUI();
		setTabHost();
	}

	private void setTabHost() {
		tabHost.addTab(tabHost.newTabSpec("TaiLieu").setIndicator("Tài liệu giảng dạy")
				.setContent(new Intent(MainActivity.this, TaiLieu.class)));
		tabHost.addTab(tabHost.newTabSpec("KiemTra").setIndicator("Kiểm tra")
				.setContent(new Intent(MainActivity.this, KiemTra.class)));
		tabHost.addTab(tabHost.newTabSpec("GiaiTri").setIndicator("Giải trí")
				.setContent(new Intent(MainActivity.this, GiaiTri.class)));
		tabHost.addTab(tabHost.newTabSpec("ThietLap").setIndicator("Thông tin")
				.setContent(new Intent(MainActivity.this, ThietLap.class)));
		tabHost.setCurrentTab(0);
	}

	private void setUI() {
		
		tvTitle = (TextView) findViewById(R.id.tvTitle);
		
		btnBaiGiang = (Button) findViewById(R.id.btnBaiGiang);
		tvTitle.setText("Tài liệu giảng dạy");
		btnBaiGiang.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				getTabHost().setCurrentTab(0);
				tvTitle.setText("Tài liệu giảng dạy");
				return false;
			}
		});

		btnKiemTra = (Button) findViewById(R.id.btnKiemTra);
		btnKiemTra.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				getTabHost().setCurrentTab(1);
				tvTitle.setText(btnKiemTra.getText().toString());
				return false;
			}
		});

		btnGiaiTri = (Button) findViewById(R.id.btnGiaiTri);
		btnGiaiTri.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				getTabHost().setCurrentTab(2);
				tvTitle.setText(btnGiaiTri.getText().toString());
				return false;
			}
		});

		btnThietLap = (Button) findViewById(R.id.btnThietLap);
		btnThietLap.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				getTabHost().setCurrentTab(3);
				tvTitle.setText(btnThietLap.getText().toString());
				return false;
			}
		});
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
		createMenu(menu);
		return true;
	}
	
	
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		return menuChoice(item);
	}

	private void createMenu(Menu menu) {
		MenuItem menuItem1 = menu.add(0, 0, 0, "Thông tin");
		{
			menuItem1.setAlphabeticShortcut('a');
			menuItem1.setIcon(R.drawable.info);
		}
		MenuItem menuItem2 = menu.add(0, 1, 1, "Thoát");
		{
			menuItem2.setAlphabeticShortcut('b');
			menuItem2.setIcon(R.drawable.exit);
		}
	}

	private boolean menuChoice(MenuItem item) {
		Intent intent;
		switch(item.getItemId()) {
		case 0:
//			intent = new Intent(MainActivity.this, SettingsActivity.class);
//			startActivity(intent);
			break;
		case 1:
			SharedPreferences prefs = getSharedPreferences("TaiKhoanGV", 0);
			String hoten = prefs.getString("hoten", "");
			String msgv = prefs.getString("msgv", "");
			
			MyApplication.getInstance().clearApplicationData();
			
			prefs = getSharedPreferences("TaiKhoanGV", 0);
			SharedPreferences.Editor editor = prefs.edit();
			editor.putString("hoten", hoten);
			editor.putString("msgv", msgv);
			editor.commit();
			
			int pid = android.os.Process.myPid();
			android.os.Process.killProcess(pid);
			System.exit(0);
			break;
		}
		
		return true;
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return false;
		}
		return false;
	}
}
